package com.example.my.shop.mybatis.controller;

import com.example.my.shop.mybatis.entity.TbUser;
import com.example.my.shop.mybatis.service.TbUserService;
import com.example.my.shop.mybatis.service.impl.TbUserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

 /**
  * @ProjectName:
  * @Package:        com.example.my.shop.mybatis.controller
  * @ClassName:      User_form_addController
  * @Description:    添加新用户功能
  * @Author:         Mr.Vincent
  * @CreateDate:     2019/6/1 15:15
  * @Version:        1.0.0
  */
@WebServlet(value = "/user_form_add")
public class User_form_addController extends HttpServlet {

    /**
     * @Method:        service
     * @Description:    获取前台form表单的参数，添加新的用户
     * @Param:         [req, resp]
     * @return:        void
     * @Author:        Mr.Vincent
     * @Date:          2019/6/1
     */
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userName = req.getParameter("userName");
        String passWord = req.getParameter("passWord");
        String phone = req.getParameter("phone");
        String email = req.getParameter("email");

        TbUserService tbUserService = new TbUserServiceImpl();
        TbUser tbUser = new TbUser(userName,passWord,phone,email);
        boolean b = tbUserService.addUser(tbUser);
        if(b) {
            req.setAttribute("message", "添加成功！！！");
        }else {
            req.setAttribute("message", "添加失败！！！");
        }
        req.getRequestDispatcher("/user_list").forward(req,resp);
    }
}
